import { Home } from 'home-model.js';
import { Product } from '../product/product-model.js';
import { ProductList } from '../product/list-model.js';
import { Cart } from '../cart/cart-model.js';
import { Article } from '../article/list-model.js';
import { Author } from '../author/author-model.js';
import { Order } from '../order/order-model.js';
import { Category } from '../category/category-model.js';
import { Fenxiao } from '../fenxiao/fenxiao-model.js';
import { Config } from '../../utils/config.js';
var order = new Order();
var home = new Home();
var product = new Product();
var productlist = new ProductList();
var cart = new Cart()
var article = new Article();
var author = new Author();
var category = new Category();
var fenxiao = new Fenxiao();

var selectIndex;
var attrIndex;
const app = getApp()
Page({
  data: {
    productData: [],
    btnChange: true,
    hiddenAttrBox: true,
    productCounts: 1,
    allselect: 0 , //sku全选中状态，1表示全选择
    firstBig:false,
    countDownList: [],
    actEndTimeList: [],
    navHeight: app.globalData.navHeight,
    page:1,
    searchLoading: false,
    searchLoadingComplete: false,
    modelAd:{
      key:'guanggaoqu'
    }
  },
  onLoad: function (option) {
    this.setData({
      domain: Config.domain
    })
    //二维码进入
    var id = '';
    if (option.scene) {
      var scene = decodeURIComponent(option.scene);
      var arr = scene.split("=");
      var id = arr[1];
    }
    if(id){
      this.fenxiaoBind(id);
    }
    this.getConfig();
    
  },

  getConfig:function(){
    var that = this;
    //获得小程序名称
    home.getXcxName((data) => {
      wx.setNavigationBarTitle({
        title: data.items[2].value
      })
      var config = data.items;
      wx.setStorageSync('config', config);
      that._loadData();
    });
  },
  //通过分销二维码进入时，发展成为下级会员
  fenxiaoBind: function (id) {
    var that = this;
    var args = {
      pid : id
    }
    fenxiao.bind(args,(data) => {
      console.log(data);
    });
  },
  /*加载所有数据*/
  _loadData: function () {
    
    var promProductParams = {
      'proming_status':1,
    }
    var that = this;
  
    //获得bannar信息
    home.getBannerData((data) => {
      that.setData({
       bannerArr: data.items,
      });
    });
    //获得导航信息
    var nav_param = {'key':'xcx_nav'}
    home.nav(nav_param,(data) => {
      that.setData({
        nav: data.items,
      });
    });

    //获取头条数据
    article.getArticleData(1, '', 1, (data) => {
      that.setData({
        hotData: data.items,
      });
    });
    
    //首页第一个横条广告
    var adv_param = {'num': 1, 'key': 'guanggaoqu' };
    home.adv(adv_param,(data) => {
      that.setData({
        adv1: data.items,
      });
    });
    //首页第二个横条广告
    var adv_param = { 'num': 1, 'key': 'index_cate_adv' };
    home.adv(adv_param,(data) => {
      that.setData({
        adv2: data.items,
        
      });
    });
    //主推热销
    this.getHotProduct();
    //获取秒杀数据
    productlist.getPromProductData('',4,promProductParams, (res) => {
      that.setData({
        promData: res.data.items,
      })
      var data = res.data.items;
      var endTimeList = [];
      for (var i = 0; i < data.length;i++){
        endTimeList.push(data[i].end_time)
      }
      this.setData({
        actEndTimeList: endTimeList
      })
      this.countDown()
    });
    //新品上市
    this.getNew();
  },
  getHotProduct:function(){
    var that = this
    var params = {
      'num': 6,
      'hot': 1,
    }
    //获取热销数据
    this.setData({
      searchLoading: true,
    })
    productlist.getProductData(this.data.page, '', params, (data) => {
      var mydata = that.data.productData.concat(data.items);
      that.setData({
        productData: mydata,
        pageCount: data._meta.pageCount,
      })
      if (data._meta.pageCount <= 1) {
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
      if (data._meta.pageCount <= that.data.page) {
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
    });
  },
  //获取新品
  getNew: function () {
    var that = this
    var params = {
      'num': 3,
      'is_new': 1,
    }
    //获取热销数据
    this.setData({
      searchLoading: true,
    })
    productlist.getProductData(1, '', params, (data) => {
      if (data.items) {
        this.setData({
          newProduct: data.items,
        })
      }
    });
  },
  // //秒杀倒计时
  countDown: function () {
    var nowtime = Date.parse(new Date()) / 1000;
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = {};
    for (var i = 0; i < this.data.actEndTimeList.length;i++){
      var endtime = this.data.actEndTimeList[i];
      var obj = null;
      var second = endtime - nowtime;
      // 天数位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;

      // 小时位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;

      // 分钟位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;

      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      countDownArr = {
        day: dayStr,
        hou: hrStr,
        min: minStr,
        sec: secStr
      }
      this.data.promData[i]['countDownArr'] = countDownArr;
    }

    this.setData({ promData: this.data.promData })
    setTimeout(this.countDown, 1000);
  },

  /*搜索*/
  showInput: function () {
    wx.navigateTo({
      url: "../product/list?search=1"
    })
  },

  /*导航跳转*/
  toNav: function (event) {
    var url = home.getDataSet(event, 'url');
    wx.navigateTo({
      url: url
    })
  },

  /*跳转到热销商品列表页*/
  onHotProductListItemTap: function (event) {
    var hot = home.getDataSet(event, 'hot');
    wx.navigateTo({
      //url: '../product/list?hot=' + hot
      url: '../product/list'
    })
  },
  onPromProductListItemTap: function (event) {
    wx.navigateTo({
      url: '../product/prom-list'
    })
  },

  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = home.getDataSet(event, 'id');
    var uid = wx.getStorageSync('uid');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },

  /*从活动列表跳转到商品详情*/
  onPromToDetailItemTap: function (event) {
    var id = home.getDataSet(event, 'id');
    var skuid = home.getDataSet(event, 'skuid');
    var uid = wx.getStorageSync('uid');
    wx.navigateTo({
      url: '../product/product?id=' + id + '&skuid=' + skuid 
    })
  },

  /*跳转到作者详情*/
  onAuthorsItemTap: function (event) {
    var id = home.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../author/author?id=' + id
    })
  },


  /*下拉刷新页面*/
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this._loadData();
    wx.hideNavigationBarLoading(),
    wx.stopPullDownRefresh()
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    var that = this;
    this.data.page++;
    if (this.data.page <= this.data.pageCount) {
      this.getHotProduct(this.data.page);
    }
  },
  /**
* 用户点击右上角分享
*/
  onShareAppMessage: function () {
    return {
      title: this.data.webTitle
    }
  },

  //跳转到作者信息列表
  getMoreAuthor:function(){
    wx.navigateTo({
      url: '../author/list'
    })
  },
  //跳转到新闻列表
  toArticleDetail:function(event){
    var id = home.getDataSet(event,'id')
    wx.navigateTo({
      url: '../article/detail?id='+ id 
    })
  },
 
  //从banner到detail
  toDetail:function(e){
    var url = home.getDataSet(e,'url');
    wx.navigateTo({
      url: url
    })
  }

})


